home *** CD-ROM | disk | FTP | other *** search
/ Univers Interactif 3 / INTERACTIF.BIN / mac / Planete.net / Internet Confirmés_Vrac / NetAgent.sea / src / agentc.c next >
C/C++ Source or Header  |  1992-06-15  |  4KB  |  155 lines

  1. /*
  2. This is an unsupported product. It was developed as part of a study at Ostfold
  3. Regional College, Norway in the spring of 1992.
  4.  
  5. You use this software at your own risk. Neither the authors nor the Ostfold
  6. Regional College nor any other person or organization except yourself is
  7. responsible for any damage or loss of data derived from the use of this
  8. software. You may distribute this software freely as long as it is in its
  9. original form and all of its files are included. If you make any changes to any
  10. part of this software or the files distributed with it, please state so and do
  11. NOT distribute it under its original name.
  12. */
  13.  
  14. /* 
  15. This is the main part of the agentc program.
  16. It sets the time between each poll to the NNTP-server and checks for new
  17. news. 
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <netdb.h>
  26. #include <time.h>
  27. #include <fcntl.h>
  28. #include "nntp_tcp.h"
  29. #include "subjects.h"
  30. #include "filelock.h"
  31.  
  32. /*
  33. -----------------------------------------------------------------------------
  34. Function name: sett_dato()
  35. -----------------------------------------------------------------------------
  36. Parameters:    char *s        Pointer to a string containing the time used
  37.                 in the command to the NNTP-server.    
  38.         long neste    Contains the time elapsed since Jan. 1, 1970
  39.                 in seconds upto the last poll.
  40. Return value:    This function returns the time in seconds, since Jan. 1 1970,
  41.         at the point of returning.
  42. Called by:    main()
  43. Calls:        none
  44. Description:    This function runs in a loop until the machine clock has
  45.         reach the value cointained in the variable neste.
  46. -----------------------------------------------------------------------------
  47. */
  48.  
  49. time_t sett_dato(s, neste)
  50. char *s;
  51. long neste;
  52. {
  53.     time_t clock = 0;
  54.     struct tm *timeptr;
  55.  
  56.     do{
  57.         time(&clock);
  58.     }while(clock < neste );
  59.  
  60.     timeptr = localtime(&clock);
  61.     strftime(s, 19, "%y%m%d %H%M%S", timeptr);
  62.  
  63.     return(clock);
  64. }
  65.  
  66. /*
  67. -----------------------------------------------------------------------------
  68. Function name: hent_nye_innlegg()
  69. -----------------------------------------------------------------------------
  70. Parameters:    int s        A valid socket descriptor.
  71.         char *d        String pointer to a string containing the
  72.                 time and date of the last poll to the NNTP-
  73.                 server.
  74. Return value:    none
  75. Called by:    main
  76. Calls:        lock_file(), unlock_file(), tcp_send(), get_list()
  77. Description:    This function call the NNTP-server askeing for the news which
  78.         has arrived since the time contained in d. It writes the id
  79.         of the articles to the New.News file.
  80. -----------------------------------------------------------------------------
  81. */
  82.  
  83. void hent_nye_innlegg(s, d)
  84. int s;
  85. char *d;
  86. {
  87.     char command[80];
  88.     char buf[BUFSIZ+1];
  89.     int f;
  90.  
  91.     f = open("New.News", O_WRONLY|O_TRUNC|O_CREAT, S_IREAD|S_IWRITE);
  92.     lock_file(f);
  93.     sprintf(command, "newnews * %s\r\n", d);
  94.     tcp_send(s, command);
  95.     get_list(s, f, 3600);
  96.     unlock_file(f);    
  97.     close(f);
  98. }
  99.  
  100. /*
  101. -----------------------------------------------------------------------------
  102. Function name: main()
  103. -----------------------------------------------------------------------------
  104. Parameters:    int argc        Number of arguments.
  105.         char *argv[]        List of arguments.
  106. Return value:    none
  107. Called by:    none
  108. Calls:        sett_dato(), tcp_connect(), get_msg(), hent_ny_innlegg(),
  109.         do_innlegg()
  110. Description:    This function runs in an infinitve loop checking for new
  111.         news. It does this by calling other functions.
  112. -----------------------------------------------------------------------------
  113. */
  114.  
  115. main( argc, argv)
  116. int argc;
  117. char *argv[];
  118. {
  119.     int s;
  120.     char command[80];
  121.     char *host;
  122.     char ny_dato[20];
  123.     char gammel_dato[20];
  124.     char *poll;
  125.     long neste;
  126.     time_t forrige;
  127.     char buf[BUFSIZ+1];
  128.  
  129.         myname = argv[0];
  130.  
  131.         if(argc != 3) {
  132.                 fprintf(stderr, "Usage: %s host poll\n", myname);
  133.                 exit(1);
  134.         }
  135.  
  136.         host = argv[1];
  137.     poll = argv[2];
  138.  
  139.     neste = atol(poll);
  140.     
  141.  
  142.     forrige = sett_dato(gammel_dato, forrige);
  143.  
  144.     while(1) {
  145.         forrige = sett_dato(ny_dato, forrige + neste);
  146.         s = tcp_connect(host, "nntp");
  147.         if(get_msg(s, '2', LINEFEED, buf, 3600) > 0) {
  148.             hent_nye_innlegg(s, gammel_dato);
  149.             do_innlegg(s);
  150.             strcpy(gammel_dato, ny_dato);
  151.         }
  152.         close(s);    
  153.     }
  154. }
  155.